package com.bdth.bdthiss.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.ArticleCategory;
import com.bdth.bdthiss.entity.Jininfo;
import com.bdth.bdthiss.entity.Jjinfo;
import com.bdth.bdthiss.entity.Product;
import com.bdth.bdthiss.service.JininfoService;
import com.bdth.bdthiss.service.ProductService;
import com.bdth.bdthiss.vo.MessageResult;
import com.bdth.bdthiss.vo.PageVo;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/compete")
@Controller
public class CompeteController {

    @Autowired
    private JininfoService jininfoService;

    @Autowired
    private ProductService productService;

    @RequestMapping("manager")
    public String manager(){
        return "compete/manager";
    }

    @RequestMapping("manager/list")
    @ResponseBody
    public PageVo<Jininfo> list(Pagination<Jininfo> pagination,String name){
        EntityWrapper<Jininfo> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("name",name);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Jininfo> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        Page<Jininfo> item = jininfoService.selectPage(page,entityWrapper);
        return new PageVo<>(item.getRecords(),item.getTotal());
    }

    @RequestMapping("product/list")
    @ResponseBody
    public PageVo<Product> listProduct(Pagination<Product> pagination, String code){
        EntityWrapper<Product> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("proId",code);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Product> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        Page<Product> item = productService.selectPage(page,entityWrapper);
        return new PageVo<>(item.getRecords(),item.getTotal());
    }

    @RequestMapping("manager/search")
    public String search(){
        return "compete/search";
    }

    @RequestMapping("manager/form")
    public String form(){
        return "compete/form";
    }

    @RequestMapping("manager/edit")
    public String edit(ModelMap map, @RequestParam int id){
        Jininfo bean = jininfoService.selectById(id);
        map.addAttribute("bean",bean);
        return "compete/edit";
    }

    @RequestMapping("manager/save")
    @ResponseBody
    public MessageResult save(Jininfo param){
        jininfoService.insert(param);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping("manager/update")
    @ResponseBody
    public MessageResult update(Jininfo param){
        jininfoService.updateById(param);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "manager/delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        jininfoService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "manager/remove")
    @ResponseBody
    public MessageResult remove(@RequestParam String ids){
        EntityWrapper<Jininfo> entityWrapper = new EntityWrapper<>();
        entityWrapper.in("id",ids);
        jininfoService.delete(entityWrapper);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "manager/ru")
    @ResponseBody
    public MessageResult ru() {
        ExcelReader reader = ExcelUtil.getReader("d:/jg.xls");
        List<Jjinfo> list = reader.read(0,1,Jjinfo.class);
        Jjinfo temp = new Jjinfo();
        for(Jjinfo info : list){
            if(info.getSn()!=null && !"".equals(info.getSn())) {
                BeanUtil.copyProperties(info, temp);
                Jininfo jTemp = new Jininfo();
                jTemp.setName(info.getName());
                jTemp.setSn(info.getSn());
                jTemp.setAddress(info.getAddress());
                jTemp.setTime(info.getTime());
                jTemp.setSendDate(info.getSendDate());
                jTemp.setPayType(info.getPayType());
                jTemp.setPayDate(info.getPayDate());
                //存储项目
                jininfoService.insert(jTemp);
                Product p = new Product();
                p.setProId(info.getSn());
                p.setProductName(info.getProductName());
                p.setProductInfo(info.getProductInfo());
                p.setProductPin(info.getProductPin());
                p.setProductSum(info.getProductSum());
                p.setProductUint(info.getProductUint());
                p.setProductType(info.getProductType());
                p.setProductValue(info.getProductValue());
                p.setDesption(info.getDesption());
                //存储 Product
                productService.insert(p);
            } else {
                Product p = new Product();
                p.setProId(temp.getSn());
                p.setProductName(info.getProductName());
                p.setProductInfo(info.getProductInfo());
                p.setProductPin(info.getProductPin());
                p.setProductSum(info.getProductSum());
                p.setProductUint(info.getProductUint());
                p.setProductType(info.getProductType());
                p.setProductValue(info.getProductValue());
                p.setDesption(info.getDesption());
                //存储 Product
                productService.insert(p);
            }
        }
        return MessageResult.getSuccess("导入成功");
    }
}
